﻿package core.image
{
    import core.enum.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;

    public class Layer extends Sprite
    {
        public var type:LayerType;
        public var editMode:Boolean;
        public var masking:Bitmap;
        public var editMask:Boolean;
        public var text:TextField;
        public var bitmap:Bitmap;
        public var shape:Sprite;
        public var holder:Sprite;
        public var alphaLock:Boolean = false;

        public function Layer(param1:LayerType = null, param2:String = "", param3:Point = null, param4:Boolean = true, param5:Number = 1, param6:Boolean = false)
        {
            this.type = param1;
            this.name = param2;
            this.alpha = param5;
            this.visible = param4;
            this.alphaLock = param6;
            if (param3 != null)
            {
                this.x = param3.x;
                this.y = param3.y;
            }
            this.holder = new Sprite();
            this.holder.cacheAsBitmap = true;
            addChild(this.holder);
            return;
        }// end function

        public function AddBitmap(core.image:Layer/Layer:BitmapData) : void
        {
            if (this.holder.numChildren > 0)
            {
                this.RemoveObject();
            }
            this.type = LayerType.Bitmap;
            this.bitmap = new Bitmap(core.image:Layer/Layer);
            this.holder.addChild(this.bitmap);
            return;
        }// end function

        public function AddText(core.image:Layer/Layer:String, core.image:Layer/Layer:int, core.image:Layer/Layer:uint, core.image:Layer/Layer:Boolean, core.image:Layer/Layer:Boolean, core.image:Layer/Layer:String = " ") : void
        {
            if (this.holder.numChildren > 0)
            {
                this.RemoveObject();
            }
            this.type = LayerType.Text;
            this.text = new TextField();
            this.text.autoSize = TextFieldAutoSize.LEFT;
            this.text.defaultTextFormat = new TextFormat(core.image:Layer/Layer, core.image:Layer/Layer, core.image:Layer/Layer, core.image:Layer/Layer, core.image:Layer/Layer);
            this.text.multiline = true;
            this.text.text = core.image:Layer/Layer;
            this.holder.addChild(this.text);
            return;
        }// end function

        public function AddMask(core.image:Layer/Layer) : void
        {
            if (this.holder.numChildren == 0)
            {
                this.AddBitmap(null);
            }
            if (core.image:Layer/Layer == null && this.bitmap.width > 0 && this.bitmap.height > 0)
            {
                core.image:Layer/Layer = new BitmapData(this.bitmap.width, this.bitmap.height, true, 16777215);
            }
            this.masking = new Bitmap(core.image:Layer/Layer);
            this.masking.cacheAsBitmap = true;
            addChild(this.masking);
            this.holder.mask = this.masking;
            this.editMask = true;
            return;
        }// end function

        public function ApplyMask() : void
        {
            var _loc_1:* = new BitmapData(this.bitmap.width, this.bitmap.height, true, 16777215);
            _loc_1.copyPixels(this.bitmap.bitmapData, this.bitmap.bitmapData.rect, this.bitmap.bitmapData.rect.topLeft, this.masking.bitmapData, this.masking.bitmapData.rect.topLeft, false);
            this.bitmap.bitmapData = _loc_1;
            this.RemoveMask();
            return;
        }// end function

        public function RemoveMask() : void
        {
            if (this.masking != null)
            {
                this.editMask = false;
                this.holder.mask = null;
                this.removeChild(this.masking);
                if (this.masking.bitmapData != null)
                {
                    this.masking.bitmapData.dispose();
                }
                this.masking = null;
            }
            return;
        }// end function

        public function RemoveObject() : void
        {
            while (this.holder.numChildren > 0)
            {
                
                this.holder.removeChildAt(0);
            }
            if (this.bitmap != null && this.bitmap.bitmapData != null)
            {
                this.bitmap.bitmapData.dispose();
            }
            this.text = null;
            this.bitmap = null;
            this.shape = null;
            return;
        }// end function

        public function SetBitmapCutEditMode(cacheAsBitmap:Selection, cacheAsBitmap:Boolean, cacheAsBitmap:Boolean) : Bitmap
        {
            var _loc_4:BitmapData = null;
            var _loc_5:Rectangle = null;
            var _loc_6:* = new Bitmap();
            if (!this.editMode && this.bitmap.bitmapData != null)
            {
                if (cacheAsBitmap == null)
                {
                    _loc_4 = this.bitmap.bitmapData.clone();
                    if (cacheAsBitmap)
                    {
                        this.bitmap.bitmapData.fillRect(_loc_4.rect, 4294967295);
                    }
                    else
                    {
                        this.bitmap.bitmapData.dispose();
                        this.bitmap.bitmapData = null;
                        _loc_6.x = _loc_6.x + this.x;
                        _loc_6.y = _loc_6.y + this.y;
                        if (this.hasMask)
                        {
                            this.masking.x = _loc_6.x;
                            this.masking.y = _loc_6.y;
                        }
                        this.x = 0;
                        this.y = 0;
                    }
                }
                else
                {
                    _loc_5 = Misc.GetAffectedRect(this.bitmap.bitmapData, cacheAsBitmap, this.offset);
                    if (_loc_5.width > 0 && _loc_5.height > 0)
                    {
                        _loc_4 = new BitmapData(_loc_5.width, _loc_5.height, true, 16777215);
                        _loc_4.copyPixels(this.bitmap.bitmapData, _loc_5, _loc_4.rect.topLeft);
                        Misc.ApplySelectionMask(_loc_4, cacheAsBitmap, new Point(_loc_5.x + this.x, _loc_5.y + this.y));
                        Misc.Clear(this.bitmap.bitmapData, cacheAsBitmap, this.offset, !cacheAsBitmap);
                        _loc_6.x = _loc_5.x;
                        _loc_6.y = _loc_5.y;
                    }
                }
                _loc_6.bitmapData = _loc_4;
                Misc.TrimBitmap(_loc_6);
                _loc_6.smoothing = cacheAsBitmap;
                this.holder.addChild(_loc_6);
                this.editMode = true;
            }
            return _loc_6;
        }// end function

        public function SetBitmapEditMode(core.enum:Number, core.enum:Rectangle) : BitmapData
        {
            var _loc_3:BitmapData = null;
            var _loc_4:Bitmap = null;
            if (!this.editMode)
            {
                this.editMode = true;
                _loc_3 = new BitmapData(core.enum.width, core.enum.height, true, 16777215);
                _loc_4 = new Bitmap(_loc_3);
                _loc_4.x = core.enum.x - this.x;
                _loc_4.y = core.enum.y - this.y;
                _loc_4.alpha = core.enum;
                if (this.editMask)
                {
                    this.addChild(_loc_4);
                }
                else
                {
                    this.holder.addChild(_loc_4);
                }
            }
            return _loc_3;
        }// end function

        public function PresetBitmapEditMode(core.image:Layer/Layer:BitmapData, core.image:Layer/Layer:Point, core.image:Layer/Layer:Number, core.image:Layer/Layer:String) : void
        {
            var _loc_5:Bitmap = null;
            if (!this.editMode)
            {
                this.editMode = true;
                _loc_5 = new Bitmap(core.image:Layer/Layer);
                _loc_5.alpha = core.image:Layer/Layer;
                _loc_5.blendMode = core.image:Layer/Layer;
                _loc_5.x = core.image:Layer/Layer.x - this.x;
                _loc_5.y = core.image:Layer/Layer.y - this.y;
                this.holder.addChild(_loc_5);
            }
            this.RemoveBitmapEditMode();
            return;
        }// end function

        public function RemoveBitmapEditMode() : void
        {
            var _loc_1:Bitmap = null;
            var _loc_2:Rectangle = null;
            var _loc_3:Sprite = null;
            var _loc_4:Rectangle = null;
            if (this.type == LayerType.Bitmap && this.editMode)
            {
                if (this.holder.numChildren > 1 || this.numChildren > 2)
                {
                    _loc_1 = (this.editMask ? (this.getChildAt(2)) : (this.holder.getChildAt(1))) as Bitmap;
                    Misc.TrimBitmap(_loc_1);
                    if (_loc_1.bitmapData != null)
                    {
                        _loc_2 = this.bitmap.getRect(this).union(_loc_1.getRect(this));
                        if (this.hasMask)
                        {
                            _loc_2 = _loc_2.union(this.masking.getRect(this));
                        }
                        if (this.bitmap.bitmapData == null)
                        {
                            this.bitmap.bitmapData = new BitmapData(_loc_2.width, _loc_2.height, true, 16777215);
                            _loc_1.x = _loc_1.x - _loc_2.x;
                            _loc_1.y = _loc_1.y - _loc_2.y;
                            this.x = _loc_2.x;
                            this.y = _loc_2.y;
                            if (this.hasMask && this.masking.bitmapData == null)
                            {
                                this.masking.bitmapData = new BitmapData(_loc_2.width, _loc_2.height, true, 16777215);
                            }
                        }
                        else if (_loc_2.width > this.bitmap.width || _loc_2.height > this.bitmap.height)
                        {
                            Misc.InflateBitmap(this.bitmap, _loc_2);
                        }
                        if (this.hasMask && (_loc_2.width > this.masking.width || _loc_2.height > this.masking.height))
                        {
                            _loc_4 = _loc_2.clone();
                            _loc_4.x = _loc_2.x - this.masking.x;
                            _loc_4.y = _loc_2.y - this.masking.y;
                            Misc.InflateBitmap(this.masking, _loc_4);
                        }
                        if (_loc_2.x < 0)
                        {
                            _loc_1.x = 0;
                            this.x = this.x + _loc_2.x;
                        }
                        if (_loc_2.y < 0)
                        {
                            _loc_1.y = 0;
                            this.y = this.y + _loc_2.y;
                        }
                        if (this.hasMask)
                        {
                            this.masking.x = 0;
                            this.masking.y = 0;
                        }
                        _loc_3 = new Sprite();
                        _loc_3.addChild(_loc_1);
                        if (this.editMask)
                        {
                            this.masking.bitmapData.draw(_loc_3);
                        }
                        else
                        {
                            this.bitmap.bitmapData.draw(_loc_3);
                        }
                        _loc_1.bitmapData.dispose();
                        _loc_1.bitmapData = null;
                    }
                    if (this.editMask && this.numChildren > 2)
                    {
                        this.removeChildAt(2);
                    }
                    else if (this.holder.numChildren > 1)
                    {
                        this.holder.removeChildAt(1);
                    }
                }
                this.editMode = false;
            }
            return;
        }// end function

        public function SetTextEditMode() : void
        {
            this.editMode = true;
            this.text.border = true;
            this.text.borderColor = 30926;
            return;
        }// end function

        public function RemoveTextEditMode() : void
        {
            this.editMode = false;
            this.text.border = false;
            return;
        }// end function

        public function get editLayer() : DisplayObject
        {
            if (this.editMode && this.editMask && this.numChildren > 2)
            {
                return this.getChildAt(2);
            }
            return this.holder.getChildAt(1);
        }// end function

        public function get activeBitmap() : BitmapData
        {
            if (this.editMask)
            {
                return this.masking.bitmapData;
            }
            return this.bitmap.bitmapData;
        }// end function

        public function get offset() : Point
        {
            var _loc_1:* = new Point();
            if (this.editMode && this.editMask && this.numChildren > 2)
            {
                _loc_1.x = Math.round(this.x + this.getChildAt(2).x);
                _loc_1.y = Math.round(this.y + this.getChildAt(2).y);
            }
            else if (this.editMode && this.holder.numChildren > 1)
            {
                _loc_1.x = Math.round(this.x + this.holder.getChildAt(1).x);
                _loc_1.y = Math.round(this.y + this.holder.getChildAt(1).y);
            }
            else
            {
                _loc_1.x = Math.round(this.x);
                _loc_1.y = Math.round(this.y);
            }
            return _loc_1;
        }// end function

        public function get hasBitmap() : Boolean
        {
            return Boolean(this.bitmap != null && this.bitmap.bitmapData != null);
        }// end function

        public function get hasMask() : Boolean
        {
            return Boolean(this.masking != null);
        }// end function

        public function Clone() : Layer
        {
            var _loc_1:TextFormat = null;
            var _loc_2:* = new Layer(this.type, this.name, new Point(this.x, this.y), this.visible, this.alpha, this.alphaLock);
            _loc_2.blendMode = this.blendMode;
            _loc_2.filters = this.CloneStyle();
            switch(this.type)
            {
                case LayerType.Bitmap:
                {
                    if (this.bitmap != null && this.bitmap.bitmapData != null)
                    {
                        _loc_2.AddBitmap(this.bitmap.bitmapData.clone());
                    }
                    else
                    {
                        _loc_2.AddBitmap(null);
                    }
                    break;
                }
                case LayerType.Text:
                {
                    if (this.text != null)
                    {
                        _loc_1 = this.text.getTextFormat();
                        _loc_2.AddText(_loc_1.font as String, _loc_1.size as int, _loc_1.color as uint, _loc_1.bold as Boolean, _loc_1.italic as Boolean, this.text.text);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this.hasMask)
            {
                _loc_2.AddMask(this.masking.bitmapData != null ? (this.masking.bitmapData.clone()) : (null));
            }
            return _loc_2;
        }// end function

        public function CloneStyle() : Array
        {
            var _loc_2:int = 0;
            var _loc_1:* = new Array();
            while (_loc_2 < this.filters.length)
            {
                
                _loc_1.push(this.filters[_loc_2]);
                _loc_2++;
            }
            return _loc_1.length > 0 ? (_loc_1) : (null);
        }// end function

    }
}
